package cn.bdqn.component;


import cn.bdqn.controller.ChargeController;
import cn.bdqn.domain.*;
import cn.bdqn.service.DoctorDiagnoseService;
import cn.bdqn.service.RecipeService;
import cn.bdqn.service.RegistrationService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;


@Component
public class HMMQReceiveDirectHandel {
    @Autowired
    private RegistrationService registrationService;
    @Autowired
    private  ChargeController chargeController;
    @Autowired
    private DoctorDiagnoseService doctorDiagnoseService;
    @Autowired
    private RecipeService recipeService;
    @Autowired
    private RedisTemplate redisTemplate;
    @RabbitListener(queues = "HM-Queue")//监听队列
    @RabbitHandler//处理
    public void getHospitalized(Hospitalized hospitalized) {
        Charge charge = new Charge();
        DoctorDiagnose doctorDiagnose=doctorDiagnoseService.queryByRegisId(hospitalized.getRegistrationId());
        Registration registration= registrationService.queryById(hospitalized.getRegistrationId());
        if(doctorDiagnose.getIsHospital()==0){
            charge.setType(1);
        }else if(doctorDiagnose.getIsHospital()==1){
            charge.setType(0);
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateHostStart = formatter.format(hospitalized.getHospStart());
        String dateHostEnd=formatter.format(hospitalized.getHostEnd());
        BigDecimal money =new BigDecimal(hospitalized.getHostMoney());
        Recipe recipe =recipeService.queryByRegisId(hospitalized.getRegistrationId());
        charge.setMenberId(registration.getMenberId());
        charge.setPayInfo("住院日期:"+dateHostStart+",出院时间:"+dateHostEnd+",住院总天数:"+hospitalized.getCountDay()+",住院押金:"+hospitalized.getFloor().getPledge()+"住院单价:"+hospitalized.getFloor().getOnedayMoney()+",处方信息:"+recipe.getRezept()+",病因:"+recipe.getEtiology());
        charge.setPayTime(new Date());
        charge.setTotalMoney(money);
        String nurseCard = (String) redisTemplate.boundValueOps(8).get();
        charge.setNurseCard(nurseCard);
        charge.setPayStatus(0);
        chargeController.addCharge(charge);
    }
}
